<?php

namespace app\services\socketio;

use think\facade\Log;
use think\swoole\Websocket;
use think\swoole\websocket\Room;

class Connect
{
    public function handle($data) {

        dump('socket_io connect start');
        dump($data);
        $ws = app(Websocket::class);
        $fd = $ws->getSender(); // 获取当前连接的 fd

        // 示例：加入房间（如根据用户ID分组）
        $roomName = 'room1';
        $ws->join($roomName); // 加入房间

        // 广播消息给房间内的所有人
        $ws->to($roomName)->emit('message', [
            'type' => 'broadcast',
            'data' => 'Hello, Room!'
        ]);

        // 获取房间内的所有客户端
        $room = app(Room::class);
        $members = $room->getClients($roomName); // 直接调用封装方法
        Log::info('房间内的成员：' . json_encode($members));

        Log::info('socket_io connected');
    }
}